<script lang="ts" setup>
import { reactive, ref, watch } from "vue"
import { deleteProductsChildrenApi, getProductsChildrenApi } from "@/api/products"
import { type GetProductsChildrenData } from "@/api/products/types"
import { ElMessage, ElMessageBox } from "element-plus"
import { RefreshRight } from "@element-plus/icons-vue"
import { usePagination } from "@/hooks/usePagination"
import { useRouter } from "vue-router"
import { getBasePath } from "@/api/common/index"
import { onMounted } from "vue"
const router = useRouter()
defineOptions({
  // 命名当前组件
  name: "ProductsChildren"
})

const loading = ref<boolean>(false)
const { paginationData, handleCurrentChange, handleSizeChange } = usePagination()

//#region 删
const handleDelete = (row: GetProductsChildrenData) => {
  ElMessageBox.confirm(`正在删除：${row.name}，确认删除？`, "提示", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning"
  }).then(() => {
    deleteProductsChildrenApi(row.id).then(() => {
      ElMessage.success("删除成功")
      getTableData()
    })
  })
}
//#endregion

//#region 改
const handleUpdate = (row: GetProductsChildrenData) => {
  router.push(`/products/childrenOperate/${row.category_id}/${row.id}`)
}
//#endregion

//#region 查
const tableData = ref<GetProductsChildrenData[]>([])
// const searchFormRef = ref<FormInstance | null>(null)
const searchData = reactive({
  name: "",
  type: ""
})
const getTableData = () => {
  loading.value = true
  getProductsChildrenApi({
    page: paginationData.page,
    size: paginationData.size,
    name: searchData.name
  })
    .then(({ data }) => {
      paginationData.total = data.total
      tableData.value = data.list
    })
    .catch(() => {
      tableData.value = []
    })
    .finally(() => {
      loading.value = false
    })
}
// const handleSearch = () => {
//   paginationData.currentPage === 1 ? getTableData() : (paginationData.currentPage = 1)
// }
// const resetSearch = () => {
//   searchFormRef.value?.resetFields()
//   handleSearch()
// }
//#endregion

/** 监听分页参数的变化 */
watch([() => paginationData.currentPage, () => paginationData.pageSize], getTableData, { immediate: true })
const basePath = ref<string>()
onMounted(async () => {
  const { data } = await getBasePath()
  basePath.value = data
})
</script>

<template>
  <div class="app-container">
    <!-- <el-card v-loading="loading" shadow="never" class="search-wrapper">
      <el-form ref="searchFormRef" :inline="true" :model="searchData">
        <el-form-item prop="name" label="产品系列名称">
          <el-input v-model="searchData.name" placeholder="请输入" />
        </el-form-item>
        <el-form-item prop="type" label="产品类型">
          <products-type-select v-model="searchData.type" placeholder="请选择" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button :icon="Refresh" @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </el-card> -->
    <el-card v-loading="loading" shadow="never">
      <div class="toolbar-wrapper">
        <!-- <div>
          <el-button type="primary" :icon="CirclePlus" @click="router.push(`/products/operate`)"
            >新增产品系列</el-button
          >
        </div> -->
        <div>
          <el-tooltip content="刷新当前页">
            <el-button type="primary" :icon="RefreshRight" circle @click="getTableData" />
          </el-tooltip>
        </div>
      </div>
      <div class="table-wrapper">
        <el-table :data="tableData">
          <el-table-column prop="name" label="产品名称" />
          <el-table-column prop="products_name" label="产品分类">
            <template #default="scope">
              {{ scope.row.products_categories.name }}
            </template>
          </el-table-column>
          <el-table-column prop="desc" label="产品描述" />
          <el-table-column prop="introduction" label="产品简介" />
          <el-table-column prop="tag" label="产品标签" />
          <!-- <el-table-column prop="name" label="建筑类型">
            <template #default="scope">
              {{ TYPE_MAP[scope.row.products.type] }}
            </template>
          </el-table-column> -->
          <el-table-column prop="usage" label="产品用途" />
          <el-table-column fixed="right" label="操作" width="300" align="center">
            <template #default="scope">
              <el-button type="primary" text bg size="small" @click="handleUpdate(scope.row)">修改</el-button>
              <el-button type="danger" text bg size="small" @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div class="pager-wrapper">
        <el-pagination
          background
          :layout="paginationData.layout"
          :page-sizes="paginationData.pageSizes"
          :total="paginationData.total"
          :page-size="paginationData.pageSize"
          :currentPage="paginationData.currentPage"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<style lang="scss" scoped>
.search-wrapper {
  margin-bottom: 20px;
  :deep(.el-card__body) {
    padding-bottom: 2px;
  }
}

.toolbar-wrapper {
  display: flex;
  justify-content: space-between;
  margin-bottom: 20px;
}

.table-wrapper {
  margin-bottom: 20px;
}

.pager-wrapper {
  display: flex;
  justify-content: flex-end;
}
</style>
